<?php
namespace App\Query;

use App\Model\User;
use App\Query\Traits\UserTrait;

class UserQuery extends \HyperfStore\Query
{
    use UserTrait;
    
    // 构建模型
    protected static function build()
    {
        return User::latest();
    }

    // 渴望加载
    protected static function with()
    {
        return ['level', 'user'];
    }
    
    // 按需加载
    public static function load($model)
    {
        return $model->load(static::with());
    }
    
    // 时间区间
    protected static function queryTime($model, $where)
    {
        return $model->whereBetween('created_at', $where);
    }
    
    // 批量获取用户
    public static function in($model, $id)
    {
        return $model->whereIn('id', $id);
    }
    
    // 获取等级同等
    public static function level($model, $level_id)
    {
        return $model->where('level_id', '>=', $level_id);
    }
    
    // 获取未锁定的用户
    public static function lockNo($model)
    {
        return $model->where('lock_status', User::LOCK_STATUS_NO);
    }
    
    // 获取满足最小区的下级
    public static function moneyUserMin($model, $money)
    {
        return $model->where('money_user', '<', $money);
    }
    
    // 获取满足最小区的用户
    public static function moneyUserMinUser($model, $money, $money_user)
    {
        return $model->whereDoesntHave('userChild', function ($query) use($money){
            static::moneyUserMin($query, $money);
        })->where('money_user', '>=', $money_user);
    }
    
    // 获取今日动态释放用户
    public static function moneyTime($model)
    {
        // 今日时间
        $time = strtotime(date("Y-m-d"),time());
        
        return $model->where('money_time', '>=', $time);
    }
    
    // 获取钱包今日需要分红的
    public static function moneyWalletTime($model = null)
    {
        // 今日时间
        $time = strtotime(date("Y-m-d"),time());
        
        return static::model($model)->where('money_wallet_time', '<', $time)->where('money_wallet', '>', 0);
    }
    
    // 获取24小时没有静态的
    public static function moneyRemainTime($model)
    {
        return $model->where('money_remain_time', '<', time() - (60 * 60 * 24));
    }
}